﻿#region The MIT License (MIT)
// The MIT License (MIT)
// Copyright (c) 2010 - 2013 Sovacode LLC
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all copies or substantial portions
// of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Serialization;
using Sovacode.RubiksCube.Common.Service;
using Sovacode.RubiksCube.Common.Service.Empty;

namespace Sovacode.RubiksCube.Common.Managers
{
    /// <summary>
    /// Base abstraction class for Managers
    /// </summary>
    /// <typeparam name="T">Mapping type</typeparam>
    public abstract class Manager<T> : IManager
    {
        #region Private Fields

        [NonSerialized] 
        [XmlIgnore] 
        private readonly IDictionary<string, T> _dictionary = new Dictionary<string, T>();

        [NonSerialized]
        [XmlIgnore] 
        private Action<string> _showError;

        [NonSerialized]
        [XmlIgnore] 
        private IWindowService _windowService;

        #endregion

        #region Properties
        /// <summary>
        /// <see cref="IManager.InvalidProperties"/>
        /// </summary>
        public abstract IReadOnlyList<string> InvalidProperties { get; }

        /// <summary>
        /// <see cref="IManager.AllProperties"/>
        /// </summary>
        public virtual IReadOnlyList<string> AllProperties
        {
            get { return Dictionary.Keys.ToList().AsReadOnly(); }
        }

        /// <summary>
        /// <see cref="IManager.ValidProperties"/>
        /// </summary>
        public abstract IReadOnlyList<string> ValidProperties { get; }

        /// <summary>
        /// Dictionary with managers data
        /// </summary>
        protected IDictionary<string, T> Dictionary
        {
            get { return _dictionary; }
        }

        /// <summary>
        /// <see cref="IManager.IsEmpty"/>
        /// </summary>
        public virtual bool IsEmpty
        {
            get { return Dictionary.Count == 0; }
        }

        #endregion

        #region Methods

        /// <summary>
        /// <see cref="IManager.Clear"/>
        /// </summary>
        public virtual void Clear()
        {
            Dictionary.Clear();
        }

        #endregion

        #region IShowError

        /// <summary>
        /// Displays an error window
        /// </summary>
        public Action<string> ShowError
        {
            protected get { return _showError ?? (_showError = s => WindowService.ShowError(s)); }
            set { _showError = value; }
        }

        /// <summary>
        /// Window service <see cref="IWindowService"/>. Needs only when Command Manager without empty <see cref="ShowError"/> parameter
        /// </summary>
        private IWindowService WindowService
        {
            get { return _windowService ?? (_windowService = new EmptyWindowService()); }
        }

        #endregion
    }
}